//// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
//package com.linqingying.gitcode.pullrequest.ui.toolwindow
//
//import com.intellij.collaboration.async.launchNow
//import com.intellij.collaboration.async.nestedDisposable
//import com.intellij.collaboration.ui.CollaborationToolsUIUtil
//import com.intellij.collaboration.ui.toolwindow.ReviewTabsComponentFactory
//import com.intellij.openapi.actionSystem.ActionManager
//import com.intellij.openapi.components.service
//import com.intellij.openapi.project.Project
//import com.intellij.util.ui.UIUtil
//import kotlinx.coroutines.CoroutineScope
//import kotlinx.coroutines.awaitCancellation
//import kotlinx.coroutines.isActive
//import kotlinx.coroutines.yield
//import com.linqingying.gitcode.authentication.accounts.GCAccountManager
//import com.linqingying.gitcode.pullrequest.GCPRStatisticsCollector
//import com.linqingying.gitcode.pullrequest.config.GitCodePullRequestsProjectUISettings
//import com.linqingying.gitcode.pullrequest.ui.list.GCPRListComponentFactory
//import com.linqingying.gitcode.pullrequest.ui.list.GCPRListPanelFactory
//import com.linqingying.gitcode.pullrequest.ui.selector.GCRepositoryAndAccountSelectorComponentFactory
//import com.linqingying.gitcode.pullrequest.ui.toolwindow.create.GCPRCreateComponentHolder
//import com.linqingying.gitcode.pullrequest.ui.toolwindow.model.GCPRToolWindowProjectViewModel
//import com.linqingying.gitcode.pullrequest.ui.toolwindow.model.GCPRToolWindowTabViewModel
//import com.linqingying.gitcode.pullrequest.ui.toolwindow.model.GCPRToolWindowViewModel
//import com.linqingying.gitcode.util.GCHostedRepositoriesManager
//import java.awt.BorderLayout
//import java.util.concurrent.CopyOnWriteArrayList
//import javax.swing.JComponent
//import javax.swing.JPanel
//import javax.swing.ListModel
//import javax.swing.event.ListDataListener
//
//internal class GCPRToolWindowTabComponentFactory(
//  private val project: Project,
//  private val vm: GCPRToolWindowViewModel
//) : ReviewTabsComponentFactory<GCPRToolWindowTabViewModel, GCPRToolWindowProjectViewModel> {
//
//  override fun createEmptyTabContent(cs: CoroutineScope): JComponent {
//    val selector = GCRepositoryAndAccountSelectorComponentFactory(project, vm.selectorVm, service<GCAccountManager>()).create(cs)
//    return JPanel(BorderLayout()).apply {
//      background = UIUtil.getListBackground()
//      add(selector, BorderLayout.NORTH)
//    }
//  }
//
//  override fun createReviewListComponent(cs: CoroutineScope, projectVm: GCPRToolWindowProjectViewModel): JComponent {
//    val ghostUser = projectVm.dataContext.securityService.ghostUser
//    val currentUser = projectVm.dataContext.securityService.currentUser
//    val listVm = projectVm.listVm
//    val listModel = cs.scopedDelegatingListModel(listVm.listModel)
//    val list = GCPRListComponentFactory(projectVm.dataContext.interactionState, listModel)
//      .create(listVm.avatarIconsProvider, ghostUser, currentUser)
//
//    GCPRStatisticsCollector.logListOpened(project)
//    return GCPRListPanelFactory(project, listVm)
//      .create(cs, list, listVm.avatarIconsProvider)
//  }
//
//  private fun <T> CoroutineScope.scopedDelegatingListModel(delegate: ListModel<T>) =
//    object : ListModel<T> by delegate {
//      private val listeners = CopyOnWriteArrayList<ListDataListener>()
//
//      init {
//        launchNow {
//          try {
//            awaitCancellation()
//          }
//          finally {
//            listeners.forEach {
//              delegate.removeListDataListener(it)
//            }
//          }
//        }
//      }
//
//      override fun addListDataListener(l: ListDataListener) {
//        if (!isActive) return
//        listeners.add(l)
//        delegate.addListDataListener(l)
//      }
//
//      override fun removeListDataListener(l: ListDataListener) {
//        delegate.removeListDataListener(l)
//        listeners.remove(l)
//      }
//    }
//
//  override fun createTabComponent(cs: CoroutineScope,
//                                  projectVm: GCPRToolWindowProjectViewModel,
//                                  tabVm: GCPRToolWindowTabViewModel): JComponent {
//    return when (tabVm) {
//      is GCPRToolWindowTabViewModel.PullRequest -> cs.createPullRequestComponent(tabVm)
//      is GCPRToolWindowTabViewModel.NewPullRequest -> cs.createNewPullRequestComponent(projectVm, tabVm)
//    }
//  }
//
//  private fun CoroutineScope.createPullRequestComponent(tabVm: GCPRToolWindowTabViewModel.PullRequest): JComponent =
//    GCPRViewComponentFactory(ActionManager.getInstance(), project, tabVm.infoVm).create(this).also { comp ->
//      launchNow {
//        tabVm.focusRequests.collect {
//          yield()
//          CollaborationToolsUIUtil.focusPanel(comp)
//        }
//      }
//    }
//
//  private fun CoroutineScope.createNewPullRequestComponent(projectVm: GCPRToolWindowProjectViewModel,
//                                                           tabVm: GCPRToolWindowTabViewModel.NewPullRequest): JComponent {
//    val repositoriesManager = project.service<GCHostedRepositoriesManager>()
//    val settings = GitCodePullRequestsProjectUISettings.getInstance(project)
//    return GCPRCreateComponentHolder(ActionManager.getInstance(), project, settings, repositoriesManager, projectVm.dataContext,
//                                     projectVm,
//                                     nestedDisposable()).component.also { comp ->
//      launchNow {
//        tabVm.focusRequests.collect {
//          yield()
//          CollaborationToolsUIUtil.focusPanel(comp)
//        }
//      }
//    }
//  }
//}
